package com.shizhanzhe.qdd.base;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.Toast;

import com.kingja.loadsir.callback.Callback;
import com.kingja.loadsir.core.LoadService;
import com.kingja.loadsir.core.LoadSir;
import com.shizhanzhe.qdd.utils.NetBroadcastReceiver;
import com.shizhanzhe.qdd.widget.ErrorCallback;

import butterknife.ButterKnife;

/**
 * Created by zz9527 on 2018/10/11.
 */
public abstract class BaseActivity extends FragmentActivity implements NetBroadcastReceiver.NetChangeListener{
    protected LoadService loadService;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(getLayout());
        ButterKnife.bind(this);
        loadService = LoadSir.getDefault().register(this, new Callback.OnReloadListener() {
            @Override
            public void onReload(View v) {
                // 重新加载逻辑
                getData();
            }
        });
        initView();
        getData();
    }
    protected abstract int getLayout();
    protected abstract void initView();

    protected abstract void getData();
    /**
     * 网络状态改变时间监听
     *
     * @param netWorkState true有网络，false无网络
     */
    @Override
    public void onNetChange(boolean netWorkState, int networkType) {
        if (netWorkState) {
            switch (networkType) {
                case NetBroadcastReceiver.NETTYPE_CMNET:
                case NetBroadcastReceiver.NETTYPE_CMWAP:
                    Toast.makeText(this, "当前使用流量", Toast.LENGTH_SHORT).show();
                    break;
                case NetBroadcastReceiver.NETTYPE_WIFI:
                    Toast.makeText(this, "当前使用WIFI", Toast.LENGTH_SHORT).show();
                    break;
                default:
                    break;
            }
            getData();
        } else {
            Toast.makeText(this, "当前无网络", Toast.LENGTH_SHORT).show();

        }
    }
}
